<?php

/**
 * Created by PhpStorm.
 * User: bill-h
 * Date: 2021/11/16
 * Time: 09:08
 * Email: 391878487@qq.com
 */
namespace app\index\middleware;


use think\Request;

/**
 * 检测用户登录
 * Class CheckIndex
 * @package app\index\middleware
 */
class CheckIndex
{

    use \app\common\traits\JumpTrait;

    public function handle(Request $request, \Closure $next)
    {
        $indexConfig = config('index');
        $userId = session('index.id');
        $expireTime = session('index.expire_time');
        /** @var AuthService $authService */

        $currentController = parse_name($request->controller());

        // 验证登录
        if (!in_array($currentController, $indexConfig['no_login_controller']) ) {
            empty($userId) && $this->error('请先登录'.$currentController, [], __url('index/login/index'));

            // 判断是否登录过期
            if ($expireTime !== true && time() > $expireTime) {
                session('index', null);
                $this->error('登录已过期，请重新登录', [], __url('index/login/index'));
            }
        }

        // 验证权限

        return $next($request);
    }

}